package com.jy.api.service.impl;

import com.jy.api.dao.VerifiedTransactionDao;
import com.jy.api.domain.VerifiedTransaction;
import com.jy.api.service.VerifiedTransactionService;
import com.jy.api.util.MyDateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by xiaohua on 2019/4/3.
 */
@Service
public class VerifiedTransactionServiceImpl implements VerifiedTransactionService {
    @Autowired
    private VerifiedTransactionDao transactionDao;

    @Override
    public Page<VerifiedTransaction> getAllVer(String storeId, String type, String begin, String end, String time, Pageable pageable) {
        Date startDate = null;
        Date endDate = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        try {
            if (StringUtils.equals(type, "DAY")) {
                startDate = MyDateUtils.getEarliest(sdf.parse(time));
                endDate = MyDateUtils.getLastest(sdf.parse(time));
            } else {
                startDate = simpleDateFormat.parse(begin);
                endDate = simpleDateFormat.parse(end);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return transactionDao.findAllByStoreIdAndVerifiedTimeBetweenOrderByVerifiedTimeDesc(storeId, startDate, endDate, pageable);
    }
}
